package com.hippo.core.ui.compose.control

import LoadingDialog
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.tuya.ThingControl
import com.hippo.core.ui.components.SecondaryButton
import com.hippo.core.ui.components.TextLayout
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.components.TitleBar
import com.hippo.core.ui.compose.mine.ChangeNameDialog
import com.hippo.core.ui.navigation.AppRoute
import com.hippo.core.ui.theme.ColorBackgroundGray
import com.hippo.core.ui.theme.ColorBackgroundPrimary
import com.hippo.core.ui.theme.ColorBackgroundWhite
import com.hippo.core.ui.theme.ColorTextBlack
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.ui.theme.PaddingMedium
import com.hippo.core.ui.theme.Shapes
import com.hippo.core.vm.DeviceSettingVM
import com.thingclips.smart.sdk.bean.DeviceBean

@Composable
fun ControlSettingScreen(navController: NavController,vm: DeviceSettingVM = viewModel()) {

    var showLoading by remember { mutableStateOf(false) }

    var showDeviceInfoDialog by remember { mutableStateOf(false) }

    var showChangeNameDialog by remember { mutableStateOf(false) }

    var showDeleteDeviceDialog by remember { mutableStateOf(false) }

    val uiState = vm.uiState

    LifecycleEventEffect(Lifecycle.Event.ON_CREATE) {
        vm.getDeviceBean()
    }


    if(showLoading){
        LoadingDialog { showLoading = false }
    }

    if(showChangeNameDialog){
        ChangeNameDialog(
            onDismissRequest = { showChangeNameDialog = false },
            onSave = {
                vm.renameDevice(it)
            }
        )
    }

    if (showDeviceInfoDialog) {
        DeviceInfoDialog(
            device = uiState.device?:DeviceBean(),
            onDismissRequest = {
                showDeviceInfoDialog = false
            }
        )
    }

    if(showDeleteDeviceDialog){
        AlertDialog(
            shape = Shapes.medium,
            onDismissRequest = {
                showDeleteDeviceDialog = false
            },
            title = {
                Text(text = stringResource(id = R.string.setting_19))
            },
            dismissButton = {
                SecondaryButton(
                    text = stringResource(id = R.string.cancel),
                    onClick = {
                        showDeleteDeviceDialog = false
                    }
                )
            },
            confirmButton = {
                ThemeButton(
                    text = stringResource(id = R.string.sure),
                    onClick = {
                        showDeleteDeviceDialog = false
                        vm.removeDevice {
                            showDeviceInfoDialog = false
                            navController.navigate(AppRoute.MAIN){
                                popUpTo(AppRoute.MAIN) {
                                    inclusive = true
                                }
                            }
                        }
                    }
                )
            }
        )
    }

    Column {

        TitleBar(
            title = stringResource(id = R.string.setting_1),
            onBack = {
                navController.popBackStack()
            }
        )

        Column(
            modifier = Modifier
                .background(ColorBackgroundPrimary)
                .padding(16.dp)
        ) {

            Column(
                modifier = Modifier
                    .background(
                        color = ColorBackgroundWhite,
                        shape = Shapes.medium
                    )
                    .padding(16.dp)
            ) {

                Image(
                    painter = painterResource(R.mipmap.machine),
                    contentDescription = null,
                    modifier = Modifier
                        .align(Alignment.CenterHorizontally)
                        .padding(PaddingMedium)
                        .size(width = 170.dp, height = 100.dp)
                )

                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(
                            color = ColorBackgroundGray,
                            shape = Shapes.extraLarge
                        )
                        .padding(12.dp)
                ) {
                    Text(
                        modifier = Modifier.align(alignment = Alignment.Center),
                        text = uiState.device?.name.orEmpty(),
                        style = TextStyle(
                            fontWeight = FontWeight.Bold,
                            fontSize = 16.sp
                        ),
                        color = ColorTextBlack
                    )


                    Image(
                        painter = painterResource(id = R.mipmap.mine_edit),
                        contentDescription = null,
                        modifier = Modifier
                            .align(alignment = Alignment.CenterEnd)
                            .size(25.dp)
                            .click {
                                showChangeNameDialog = true
                            }
                    )

                }

            }

            Spacer(modifier = Modifier.weight(1F))

            TextLayout(
                leftPainter = painterResource(id = R.mipmap.setting_info),
                text = stringResource(R.string.setting_15),
                onClick = {
                    showDeviceInfoDialog = true
                }
            )

            TextLayout(
                leftPainter = painterResource(id = R.mipmap.setting_time),
                text = stringResource(R.string.setting_17),
                onClick = {
                    navController.navigate(AppRoute.CleanScreen)
                }
            )

            TextLayout(
                leftPainter = painterResource(id = R.mipmap.setting_manage),
                text = stringResource(R.string.setting_18),
                onClick = {
                    navController.navigate(AppRoute.ConsumablesScreen)
                }
            )

            Spacer(modifier = Modifier.weight(2F))

            SecondaryButton(
                text = stringResource(id = R.string.setting_19),
            ) {
                showDeleteDeviceDialog = true
            }

            Spacer(modifier = Modifier.weight(2F))


        }
    }


}


@Preview
@Composable
private fun DeviceInfoScreenPreview() {
    HippoTheme {
        ControlSettingScreen(
            navController = NavController(LocalContext.current)
        )
    }
}